﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using ParkingLot.Entitys.Enums;
using ParkingLot.Entitys.Messages;
using ParkingLot.Utility.Extend;
using ParkingLot.Utility.LogHelps;

namespace ParkingLot.Utility.MessageHelp
{
    /*设计模式之代理人模式和工厂模式结合   代理人模式:MeassageAgrate不认识UDP 通过中介类认识*/

    /// <summary>
    ///    
    /// </summary>
    public class MessageProxy : System.Windows.Threading.DispatcherObject
    {
        
        #region 字段

        public readonly string MessageVirtualIP;

        public readonly int MessagePort;

        /// <summary>
        /// 类和他的方法
        /// </summary>
        public  Dictionary<Guid, MethodInfo> DcitMethodInfos=new Dictionary<Guid, MethodInfo>(); 

        /// <summary>
        /// 缓存推送的对象序列化之后的流
        /// </summary>
        private  ConcurrentQueue<byte[]> m_CacheBytes=new ConcurrentQueue<byte[]>(); 
        #endregion
       
        

        public static void AddMethInfo(Guid guid, MethodInfo methodInfo)
        {
           
        }

        public static void RemoveMethInfo(Guid guid)
        {
            
        }

        private void AddBytes(byte[] bytes)
        {
            this.m_CacheBytes.Enqueue(bytes);
        }


        private void DealMessageAction(byte[] buff)
        {
            ICommunicationMessage mb = (ICommunicationMessage)SerializeHelp.ByteFlowHelp.ByteToObj(buff);

            //执行方法
            if (DcitMethodInfos.Keys.Contains(mb.ClassId))
            {
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
                {
                    DcitMethodInfos[mb.ClassId].Invoke(null, new object[1] { mb });
                }));
            }
        }


    }
}
